package cn.com.sbell.vss.csg.vsclient.ipcamera;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;

import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.csg.vap.bean.PtzCommander;
import cn.com.sbell.vss.csg.vsclient.util.BeanValidator;

public class IpCameraPtzCommandSet {
	private static Map commandMap;
	static {
		commandMap = Collections.synchronizedMap(new HashMap());
		Properties prop = new Properties();
		try {
			prop.load(IpCameraPtzCommandSet.class
					.getResourceAsStream("IpCameraPtzCommander.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		Enumeration nameEnum = prop.propertyNames();
		while (nameEnum.hasMoreElements()) {
			String property = (String) nameEnum.nextElement();
			String value = null;

			value = prop.getProperty(property);

			commandMap.put(property, value);
		}
	}

	public static String[] getPtzCommand(String ptzcmd) {
		String[] cmd = null;
		String command = (String) commandMap.get(ptzcmd);
		if (command == null) {
			throw new VssException(
					VssConstants.CODE_UNSUPPORTED_PTZ_PROTOCOL_COMMAND);
		}
		cmd = command.split(":");
		return cmd;
	}

	public static HttpMethod createCommandMethod(String[] cmd,
			PtzCommander ptzCmder, String modelType) {
		GetMethod method = null;
		Object[] fillPresetPosition = null;
		if (GetIpCameraVersion.getType(modelType)) {
			if (VssConstants.RESULT_NORMAL_STATUS.equals(cmd[0])) {
				method = new GetMethod(cmd[2]);
			} else if (VssConstants.WEB_PAGE_STATUS.equals(cmd[0])) {
				BeanValidator.validateNotNull(ptzCmder,
						new String[] { "param" });
				if (VssConstants.RESULT_NORMAL_STATUS.equals(ptzCmder
						.getParam())) {
					method = new GetMethod(cmd[3]);
				} else {
					fillPresetPosition = new Object[] { ptzCmder.getParam() };
					MessageFormat form = new MessageFormat(cmd[2]);
					method = new GetMethod(form.format(fillPresetPosition));
				}

			} else {
				if (ptzCmder.getCmd().equals("AUTO_START_LR")
						|| ptzCmder.getCmd().equals("AUTO_START_UD")) {
					method = new GetMethod(cmd[1]);
				} else {
					method = new GetMethod(cmd[2]);
				}
			}
		} else {
			if (VssConstants.RESULT_NORMAL_STATUS.equals(cmd[0])) {
				if (VssConstants.MODEL_10TYPE.equals(modelType)
						|| VssConstants.MODEL_TYPE.equals(modelType)) {
					method = new GetMethod(cmd[2]);
				} else {
					method = new GetMethod(cmd[1]);
				}

			} else if (VssConstants.WEB_PAGE_STATUS.equals(cmd[0])) {
				BeanValidator.validateNotNull(ptzCmder,
						new String[] { "param" });
				fillPresetPosition = new Object[] { ptzCmder.getParam() };
				MessageFormat form = new MessageFormat(cmd[1]);
				method = new GetMethod(form.format(fillPresetPosition));
			} else {
				method = new GetMethod(cmd[1]);
			}
		}

		return method;
	}

	public static void main(String[] args) {
		String[] a = IpCameraPtzCommandSet.getPtzCommand("ZIN");
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}
	}
}
